<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户" prop="clientId">
        <el-select v-model="queryParams.clientId" clearable placeholder="请选择客户">
          <el-option
            v-for="item in clientList"
            :key="item.clientId"
            :label="item.clientName"
            :value="item.clientId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="产品" prop="mdItemId">
        <el-cascader
          v-model="queryParams.mdItemId"
          :options="itemTreeQuery"
          clearable
          :show-all-levels="false"
          placeholder="请选择产品"
          :props="defaultProps"
          @change="listTreeCurrentChangeHandle($event)">
          <template slot-scope="{ node, data }">
            <span>{{ data.label }}</span>
            <span v-if="!node.isLeaf">({{ data.children.length }})</span>
          </template>
        </el-cascader>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ps:survey:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ps:survey:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ps:survey:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="surveyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客户名称" align="center" prop="clientId" :formatter="clientFormatter" />
      <el-table-column label="产品名称" align="center" prop="mdItemId" :formatter="itemTreeFormatter" />
      <el-table-column label="产品是否满足您的需求？" align="center" prop="answerA" />
      <el-table-column label="产品的质量是否符合您的期望？" align="center" prop="answerB" />
      <el-table-column label="您对产品的耐用性如何？" align="center" prop="answerC" />
      <el-table-column label="产品的价格是否合理？" align="center" prop="answerD" />
      <el-table-column label="您会推荐该产品给他人吗？" align="center" prop="answerE" />
      <el-table-column label="产品的售后服务是否及时和周到？" align="center" prop="answerF" />
      <el-table-column label="您对产品的性价比如何？" align="center" prop="answerG" />
      <el-table-column label="您对产品的整体满意度如何？" align="center" prop="answerH" />
      <el-table-column label="您对产品的改进意见或建议是什么？" align="center" prop="suggest" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="handleInfo(scope.row)"
            v-hasPermi="['ps:survey:query']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['md:survey:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['md:survey:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <!-- 添加或修改客户调查对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-position="top" label-width="80px">
        <el-form-item label="1.请选择客户" prop="clientId">
          <el-select v-model="form.clientId" clearable placeholder="请选择客户">
            <el-option
              v-for="item in clientList"
              :key="item.clientId"
              :label="item.clientName"
              :value="item.clientId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="2.请选择产品" prop="mdItemId">
          <el-cascader
            v-model="form.mdItemId"
            :options="itemTreeAdd"
            clearable
            :show-all-levels="false"
            placeholder="请选择产品"
            :props="defaultProps"
            @change="listTreeCurrentChangeHandle2($event)">
            <template slot-scope="{ node, data }">
              <span>{{ data.label }}</span>
              <span v-if="!node.isLeaf">({{ data.children.length }})</span>
            </template>
          </el-cascader>
        </el-form-item>
        <el-form-item label="3.产品是否满足您的需求？">
          <el-radio-group v-model="form.answerA">
            <el-radio label="是">是</el-radio>
            <el-radio label="否">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="4.产品的质量是否符合您的期望？">
          <el-radio-group v-model="form.answerB">
            <el-radio label="是">是</el-radio>
            <el-radio label="否">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="5.您对产品的耐用性如何？">
          <el-radio-group v-model="form.answerC">
            <el-radio label="非常耐用">非常耐用</el-radio>
            <el-radio label="耐用">耐用</el-radio>
            <el-radio label="一般">一般</el-radio>
            <el-radio label="不耐用">不耐用</el-radio>
            <el-radio label="非常不耐用">非常不耐用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="6.产品的价格是否合理？">
          <el-radio-group v-model="form.answerD">
            <el-radio label="是">是</el-radio>
            <el-radio label="否">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="7.您会推荐该产品给他人吗？">
          <el-radio-group v-model="form.answerE">
            <el-radio label="是">是</el-radio>
            <el-radio label="否">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="8.产品的售后服务是否及时和周到？">
          <el-radio-group v-model="form.answerF">
            <el-radio label="是">是</el-radio>
            <el-radio label="否">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="9.您对产品的性价比如何？">
          <el-radio-group v-model="form.answerG">
            <el-radio label="非常高">非常高</el-radio>
            <el-radio label="高">高</el-radio>
            <el-radio label="一般">一般</el-radio>
            <el-radio label="低">低</el-radio>
            <el-radio label="非常低">非常低</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="10.您对产品的整体满意度如何？">
          <el-radio-group v-model="form.answerH">
            <el-radio label="非常高">非常满意</el-radio>
            <el-radio label="高">满意</el-radio>
            <el-radio label="一般">一般</el-radio>
            <el-radio label="低">不满意</el-radio>
            <el-radio label="非常低">非常不满意</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="您对产品的改进意见或建议是什么？" prop="suggest">
          <el-input v-model="form.suggest" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看对话框 -->
    <el-dialog :visible.sync="infoOpen">
      <el-descriptions :title="title" :column="1" border>
        <el-descriptions-item label="1.客户名称">{{this.infoList.clientName}}</el-descriptions-item>
        <el-descriptions-item label="2.产品名称">{{this.infoList.mdItemName}}</el-descriptions-item>
        <el-descriptions-item label="3.产品是否满足您的需求？">{{this.infoList.answerA}}</el-descriptions-item>
        <el-descriptions-item label="4.产品的质量是否符合您的期望？">{{this.infoList.answerB}}</el-descriptions-item>
        <el-descriptions-item label="5.您对产品的耐用性如何？">{{this.infoList.answerC}}</el-descriptions-item>
        <el-descriptions-item label="6.产品的价格是否合理？">{{this.infoList.answerD}}</el-descriptions-item>
        <el-descriptions-item label="7.您会推荐该产品给他人吗？">{{this.infoList.answerE}}</el-descriptions-item>
        <el-descriptions-item label="8.产品的售后服务是否及时和周到？">{{this.infoList.answerF}}</el-descriptions-item>
        <el-descriptions-item label="9.您对产品的性价比如何？">{{this.infoList.answerG}}</el-descriptions-item>
        <el-descriptions-item label="10.您对产品的整体满意度如何？">{{this.infoList.answerH}}</el-descriptions-item>
        <el-descriptions-item label="您对产品的改进意见或建议是什么？">{{this.infoList.suggest}}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
import { listSurvey, getSurvey, delSurvey, addSurvey, updateSurvey } from "@/api/mes/ps/survey";
import { listClient } from "@/api/mes/md/client"
import { getTree, getAll } from "@/api/mes/md/mdItem"

export default {
  name: "Survey",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户调查表格数据
      surveyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //
      infoOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        clientId: null,
        mdItemId: null,
      },
      // 表单参数
      form: {},
      // 客户list
      clientList: [],
      // 产品树
      itemTreeQuery: [],
      itemTreeAdd: [],
      defaultProps: {
        label: 'label',
        value: 'id',
        children: 'children'
      },
      infoList: [],
      itemList: [],
      // 表单校验
      rules: {
        clientId: [{ required: true, message: "客户ID不能为空", trigger: "blur" }],
        mdItemId: [{ required: true, message: "产品ID不能为空", trigger: "blur" }],
      }
    };
  },
  created() {
    this.getList();
    this.getClientList();
    this.getItemTree();
    this.getItemAll();
  },
  methods: {
    /** 查询客户调查列表 */
    getList() {
      this.loading = true;
      listSurvey(this.queryParams).then(response => {
        this.surveyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        surveyId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        clientId: null,
        mdItemId: null,
        answerA: "0",
        answerB: "0",
        answerC: "0",
        answerD: "0",
        answerE: "0",
        answerF: "0",
        answerG: "0",
        answerH: "0",
        answerI: "0",
        answerJ: "0",
        suggest: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.surveyId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户调查";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const surveyId = row.surveyId || this.ids
      getSurvey(surveyId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户调查";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.surveyId != null) {
            updateSurvey(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSurvey(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const surveyIds = row.surveyId || this.ids;
      this.$modal.confirm('是否确认删除客户调查数据？').then(function() {
        return delSurvey(surveyIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 查看按钮操作 **/
    handleInfo(row) {
      this.infoOpen = true;
      this.title = "客户体验满意度调查";
      row.clientName = this.clientFormatter(row)
      row.mdItemName = this.itemTreeFormatter(row)
      this.infoList = row;
    },
    /** 查询客户列表 **/
    getClientList() {
      listClient().then(response => {
        this.clientList = response.rows
      });
    },
    /** 客户转换 **/
    clientFormatter(data) {
      for (let i = 0; i < this.clientList.length; i++) {
        if (this.clientList[i].clientId === data.clientId) {
          return this.clientList[i].clientName
        }
      }
    },
    /** 查询产品整体树 **/
    getItemTree() {
      getTree().then(response => {
        this.itemTreeQuery = response.data
        this.itemTreeAdd = response.data
      });
    },
    /** 查询全部产品 **/
    getItemAll() {
      getAll().then(response => {
        this.itemList = response.rows
      })
    },
    /** 产品树转换 **/
    itemTreeFormatter(data) {
      for (let i = 0; i < this.itemList.length; i++) {
        if (this.itemList[i].itemId === data.mdItemId) {
          return this.itemList[i].itemName
        }
      }
    },
    /** 查询树节点过滤 **/
    listTreeCurrentChangeHandle(selectedNodes) {
      if (selectedNodes !== undefined) {
        this.queryParams.mdItemId = selectedNodes.slice(-1)[0]
      }
    },
    /** 新增/修改树节点过滤 **/
    listTreeCurrentChangeHandle2(selectedNodes) {
      if (selectedNodes !== undefined) {
        this.form.mdItemId = selectedNodes.slice(-1)[0]
      }
    },
  }
};
</script>
